//
// Created by 86138 on 2025/6/17.
//
#include "SSTable.h"
#include <iostream>

namespace sweetcookie9_1 {

    using namespace std;

    void CreateFromFile(SSTable &ST, char *filename) {
        FILE *f;
        f = fopen(filename, "r");
        fscanf(f, "%d", &ST.length);
        ST.elem = (ElemType *) calloc(ST.length + 1, sizeof(ElemType));
        if (!ST.elem) exit(OVERFLOW);

        for (int i = 1; i <= ST.length; i++) {
            InputFromFile(f, ST.elem[i]);
        }
        fclose(f);
    }

    void InputFromFile(FILE* f, ElemType &c)
    { fscanf(f, "%ld%s%d%d%d%d%d%d%d", &c.number, c.name, &c.politics,
             &c.chinese, &c.english, &c.math, &c.physics, &c.chemistry, &c.biology);
    }

    void Traverse(SSTable ST, void(*Visit)(ElemType)) {
        ElemType *p = ++ST.elem;
        for (int i = 1; i <= ST.length; i++) {
            Visit(*p++);
        }
    }

    void Visit(ElemType c)
    { printf("%-8ld%-8s%5d%5d%5d%5d%5d%5d%5d%5d\n", c.number, c.name,
             c.politics, c.chinese, c.english, c.math, c.physics, c.chemistry,
             c.biology, c.total);
    }

    int Search(SSTable ST, KeyType key) {
        ST.elem[0].key = key;
        int i;
        for (i = ST.length; ST.elem[i].key != key; i--)
            ;
        return i;
    }
}

//using namespace sweetcookie9_1;
//
//int main() {
//    system("chcp 65001");
//    SSTable ST;
//    CreateFromFile(ST, "D:\\playground5\\qingdao-university\\09-search\\f8-1.txt");
//    Traverse(ST, Visit);
//
//    cout << "请输入待查找人的准考证号：";
//    long s;
//    cin >> s;
//    int i = Search(ST, s);
//    if (i) {
//        cout << "找到了，";
//        Visit(ST.elem[i]);
//    } else {
//        cout << "没有找到" << endl;
//    }
//
//    return 0;
//}